//
//  LogonOptionTableViewDelegateSource.m
//  Rongcloud
//
//  Created by LiuLinhong on 2017/09/06.
//  Copyright © 2017年 Beijing Rongcloud Network Technology Co. , Ltd. All rights reserved.
//

#import "LogonOptionTableViewDelegateSource.h"
#import "LogonViewController.h"
#import "LogonOptionTableViewCellView.h"

@interface LogonOptionTableViewDelegateSource ()

@property (nonatomic, strong) LogonViewController *logonViewController;

@end

@implementation LogonOptionTableViewDelegateSource

- (instancetype)initWithViewController:(NSViewController *)vc
{
    self = [super init];
    if (self)
    {
        self.logonViewController = (LogonViewController *) vc;
        self.cellDic = [NSMutableDictionary dictionary];
    }
    return self;
}

#pragma mark - NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return 2;
}

#pragma mark - NSTableViewDelegate
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    LogonOptionTableViewCellView *cell = (LogonOptionTableViewCellView *)self.cellDic[@(row)];
    
    if (!cell)
    {
        cell = [tableView makeViewWithIdentifier:@"LogonOptionTableViewCellView" owner:self];
        cell.cellSwitch.row = row;
        cell.cellSwitch.target = self.logonViewController;
        cell.cellSwitch.action = @selector(logonOptionSwitchAction:);
        self.cellDic[@(row)] = cell;
        switch (row)
        {
            case 0:
            {
                cell.cellTextField.stringValue = NSLocalizedString(@"logon_join_close_camera", nil);
                if (self.logonViewController.closeCameraIndex == RongRTC_User_Only_Audio)
                    [cell.cellSwitch setOn:YES];
            }
                break;
            case 1:
            {
                cell.cellTextField.stringValue = NSLocalizedString(@"logon_resolution_ratio", nil);
                [cell.cellSwitch removeFromSuperview];
                
                NSButtonCell *buttonCell = [NSButtonCell new];
                [buttonCell setButtonType: NSRadioButton];
                [buttonCell setBordered: NO];
                [buttonCell setControlSize:NSControlSizeRegular];
                
                NSMatrix *resolutionMatrix = [[NSMatrix alloc] initWithFrame:CGRectMake(50, 0, 250, 40) mode:NSRadioModeMatrix prototype:buttonCell numberOfRows:2 numberOfColumns:3];
                resolutionMatrix.cellSize = CGSizeMake(83, 20);
                [cell addSubview:resolutionMatrix];
                
                buttonCell = [resolutionMatrix cellAtRow: 0 column: 0];
                [buttonCell setTitle: @"320*240"];
                [buttonCell setTag: NSRadioModeMatrix];
                buttonCell.target = self.logonViewController;
                buttonCell.action = @selector(resolutionRadioButtonAction:);
                
                buttonCell = [resolutionMatrix cellAtRow: 0 column: 1];
                [buttonCell setTitle: @"640*480"];
                [buttonCell setTag: NSRadioModeMatrix];
                buttonCell.target = self.logonViewController;
                buttonCell.action = @selector(resolutionRadioButtonAction:);
                
                buttonCell = [resolutionMatrix cellAtRow: 0 column: 2];
                [buttonCell setTitle: @"1280*720"];
                [buttonCell setTag: NSRadioModeMatrix];
                buttonCell.target = self.logonViewController;
                buttonCell.action = @selector(resolutionRadioButtonAction:);
                
                buttonCell = [resolutionMatrix cellAtRow: 1 column: 0];
                [buttonCell setTitle: @"1920*1080"];
                [buttonCell setTag: NSRadioModeMatrix];
                buttonCell.target = self.logonViewController;
                buttonCell.action = @selector(resolutionRadioButtonAction:);

                NSCell *tmpCell1 = [[NSCell alloc] init];
                [tmpCell1 setSelectable:NO];
                [resolutionMatrix putCell:tmpCell1 atRow:1 column:1];
                
                NSCell *tmpCell2 = [[NSCell alloc] init];
                [tmpCell2 setSelectable:NO];
                [resolutionMatrix putCell:tmpCell2 atRow:1 column:2];
                
                [resolutionMatrix selectCell:[resolutionMatrix cellAtRow: 0 column:self.logonViewController.resolutionRatioIndex]];
            }
                break;
                
            default:
                break;
        }
    }
    return cell;
    
}

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
    if (row == 1)
        return 40;
    else
        return 20;
}

- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
{
    return NO;
}


@end
